package com.karst.essentials.chapter06
{
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.CurveModifiers;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Cylinder;

	internal class TweenerCameraExample extends InteractionTemplate
	{
		private var pointOfView : String = "yellow" ;
		
		public function TweenerCameraExample()
		{
			super( true ) ;
			addEvents() ;
			CurveModifiers.init() ;
		}
		
		private var light : PointLight3D = null ;
		private var redCylinder : Cylinder = null ;
		private var yellowCylinder : Cylinder = null ;
		override protected function createView():void
		{
			if( light == null )
				light = new PointLight3D() ;
			light.y = 1000 ;
			
			if( redCylinder == null )
				redCylinder = new Cylinder( new FlatShadeMaterial( light,
					0xFF0000, 0x000000 ), 30, 120, 20, 20 ) ;
			redCylinder.z = -100 ;
			
			if( yellowCylinder == null )
				yellowCylinder = new Cylinder( new FlatShadeMaterial( light,
					0xFFCC00, 0x000000 ), 20, 120, 20, 20 ) ;
			yellowCylinder.z = 100 ;
			
			scene.addChild( redCylinder ) ;
			scene.addChild( yellowCylinder ) ;
			
			camera.x = -10 ;
			camera.y = 100 ;
			camera.z = -300 ;
		}

		private function addEvents() : void
		{
			addEventListener( MouseEvent.CLICK, clickHandler ) ;
		}
		
		private function clickHandler( $event : Event ) : void
		{
			if( !Tweener.isTweening( camera ) )
			{
				switch( pointOfView )
				{
					case "yellow" :
						Tweener.addTween( camera, 
							{ x : -80, 
							  y : 150,
							  z : 300,
							  _bezier : {x : -1000, y : 800, z : 1000 },
							  time : 2, 
							  transition : "easeInOutQuad",
							  onComplete : setPointOfView,
							  onCompleteParams : ["red"] 
							} ) ;
						break ;
					case "red" :
						Tweener.addTween(camera,
							{ x : -50, 
							  y : 100,
							  z : -300,
							  _bezier : {x : 600, y : 2000, z : -1000 },
							  time : 2, 
							  transition : "easeInOutQuad",
							  onComplete : setPointOfView,
							  onCompleteParams : ["yellow"] 
							});
						break ;
					default :
						break ;
				}
			}
		}
		
		private function setPointOfView( $view : String ) : void
		{
			pointOfView = $view ;
		}
	}
}